<?php
/**
 * 支付相关接口
 * */

namespace App\Http\Controllers;

use App\Http\Model\FinanceRechargeOrder;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Model\System;
use App\Http\Model\User;
use App\Http\Services\FinancePayServices;
use App\Http\Services\FinanceRechargeOrderServices;
use App\Http\Services\FinanceServices;
use App\Http\Services\MsgServices;
use App\Http\Services\UserBlacklistServices;
use App\Http\Services\UserServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Lib\Response;
use Illuminate\Support\Facades\Redis;

class FinancePayController extends Controller
{
    protected $request;
    protected $service;
    protected $payService;
    protected $config;
    protected $platformConf = [1, 2];//1:iOS 2:android
    private $services_user;
    private $services_msg;
    private $services_finance;
    private $model_finance_recharge_order;
    private $services_black;
    private $userModel;
    protected $model_system;

    protected $payOrderLimitReidsKey = 'PAY:ORDER:FINASH:USER:';//string 用户成功支付渠道支付间隔
    protected $payOrderFiveMinutes = 'PAY:ORDER:FIVE:MINUTES:';//string 用户未支付3次缓存

    public function __construct(Request $request, FinanceRechargeOrderServices $rechargeOrderServices, FinancePayServices $payServices, UserServices $userServices, MsgServices $msgServices, FinanceServices $financeServices)
    {
        $this->request = $request;
        $this->config = config('pay');
        $this->service = $rechargeOrderServices;
        $this->payService = $payServices;
        $this->services_user = $userServices;
        $this->services_msg = $msgServices;
        $this->services_finance = $financeServices;
        $this->userModel = new User();
        $this->model_system = new System();
        $this->model_finance_recharge_order = new FinanceRechargeOrder();
        $this->services_black = new UserBlacklistServices();
    }


    /**
     * @desc 本地订单创建&发起支付
     * @param $rule_id 充值规则
     * @param $pay_type 0:支付宝 1:微信 2：苹果
     * @param $plat_type 2:android 1:iOS
     * @param
     * @date 2019/1/7
     * */
    public function pay_unified_middleware()
    {
        try {
            $rule_id = (int)$this->request->input('rule_id');//规则id
            $pay_type = (int)$this->request->input('pay_type');//支付类型	1=支付宝 2=微信 3=苹果 6=支付宝h5 7=微信H5 8=公众号
            $plat_type = (int)$this->request->input('plat_type');//客户端类型 1=iOS 2=android 3=apple内购 4=公众号 5=后台充值 6=H5
            $code = $this->request->input('code'); //微信公众鉴权code
            $custom_price = (int)$this->request->input('custom_price') ?? 0; //自定义充值金额
            $rechargeType = (int)$this->request->input('recharge_type', 0); //充值类型 0=钻石 1=

            //获取目前开启的支付类型
            $config = $this->model_system->get_config();
//        $list = empty($config['pay_type_list']) ? [] : $config['pay_type_list']['value'];
            //添加支付异常提示 add by zxiaofee
//        $userId=10010;
            $userId = $this->services_user->get_current_user_id() ?? 0;
            if ($userId <= 0) {

                Response::returnResult(Hint::LOGIN_OUT);
            }
//
//        $paystoreid=empty($config['pay_store_list']) ? [] : $config['pay_store_list']['value'];
//        $paystoremd5=empty($config['pay_store_md5']) ? [] : $config['pay_store_md5']['value'];
//        $paystoreaes=empty($config['pay_store_aes']) ? [] : $config['pay_store_aes']['value'];
//        if(!empty($list)) {
//            $list = json_decode($list, true);
//        }
            $list = $this->services_finance->setpayTypeList($userId);
            if (empty($list)) {
                $list = empty($config['pay_type_list']) ? [] : $config['pay_type_list']['value'];

            }
            if (!in_array($pay_type, $list)) {
                Response::returnResult(Hint::NOT_ALLOWED, [], '支付渠道维护中!');
            }


            //单比最多充值限制
            if ($custom_price > 100000) {
                Response::returnResult(Hint::FINANCE_RECHARGE_PAY_MAX);
            }

            //获取未实名用户充值限制
            $limited = $this->service->checkUserConfirmStatus($userId, $rule_id);
            if (!$limited) {
                Response::returnResult(33, [], '如需充值请先实名认证!');
            }

            //白名单
            // $testUser = $this->userModel->get_test_users();
            // if(!in_array($userId,['10003'])){
            //     Response::returnResult(Hint::PARAM_ERROR, [], '支付维护中,请稍后~');
            // }

            //支付宝问题
            // if($pay_type == 1) {
            //     Response::returnResult(Hint::PARAM_ERROR, [], '充值请加代充微信：wsl55555888');
            // }
            // //微信问题
            // if($pay_type == 2) {
            //     Response::returnResult(Hint::PARAM_ERROR, [], '代充请加微信：wsl55555888');
            // }

            //充值完成间隔渠道判断
            if (!empty(Redis::GET($this->payOrderLimitReidsKey . $userId . ':PAYTYPE:' . $pay_type))) {
                //Response::returnResult(Hint::NOT_ALLOWED,[],'操作频繁，请稍后重试');
            }

            //充值未支付三次5分钟间隔判断
            if (!empty(Redis::GET($this->payOrderFiveMinutes . $userId . ':PAYTYPE:' . $pay_type))) {
                //Response::returnResult(Hint::NOT_ALLOWED,[],'请勿连续未支付订单，5分钟后可自动解除充值限制');
            }

            //充值永久黑名单
            $payBlack = $this->service->checkUserInRechargeBlack($userId, $pay_type);
            if ($payBlack) {
                Response::returnResult(Hint::FINANCE_RECHARGE_BLACK, [], '当前支付通道异常，请您稍后再试');
            }

            if (!in_array($pay_type, [1, 2, 3, 6, 9, 10, 11, 12, 13, 14, 15,19, 20, 21,25,26])) $pay_type = 0;
            if ($rule_id === null || empty($pay_type) || $plat_type == null) {
                Response::returnResult(Hint::PARAM_ERROR);
            }
            init_redis_session();
            if (!isset($_SESSION['user_id'])) {
                $this->service->checkToken();
            }
            if (!empty($custom_price)) {
                $rule_id = 0;
                if ($custom_price < 12) {
                    Response::returnResult(Hint::PARAM_ERROR, [], '抱歉，自定义金额最低充值12元');
                }
                if ($custom_price > 4999) {
                    Response::returnResult(Hint::PARAM_ERROR, [], '抱歉，自定义金额最多充值4999元');
                }
            }
            //根据pay_type读取流水id，和支付配置名称
            $payConfig = $this->payService->getConfigByPayType($pay_type);
            if (empty($payConfig)) {
                Response::returnResult(Hint::FINANCE_RECHARGE_TYPE_ERROR);
            }
            write_log('payyy', "aaa", $payConfig);
            $createRes = $this->service->createRechargeOrder($userId, $rule_id, $payConfig['stream_type'], $plat_type, $rechargeType, $custom_price, $payConfig['config_name']);
            if (is_array($createRes) && !empty($createRes['orderId'])) {
                $order_id = $createRes['orderId'];
            } else {
                if (is_numeric($createRes) && $createRes > 0) {
                    Response::returnResult($createRes);
                } else {
                    Response::returnResult(Hint::WAIT_OPEN);
                }
            }

            $order_info = $this->service->getRechargeOrder($order_id);
            if ($order_info == false || empty($order_info)) {
                Response::returnResult(Hint::ERROR);
            }
            if (!empty($code)) {
                $order_info['code'] = $code;
            }
            if ($pay_type == 15) {
                if ($order_info['price'] > 100000) {
                    Response::returnResult(Hint::PARAM_ERROR, [], '抱歉，金额充值大于1000元,不能充值');
                }
            }
            $result = [];
            switch ($pay_type) {
                case 1://支付宝app
                    $result = $this->payService->orderAlipayApp($payConfig['config_name'], $order_info);
                    break;
                case 2://微信app
                    $result = $this->payService->unifiedOrderWxApp($payConfig['config_name'], $order_info);
                    break;
                case 3://苹果
                    $result = $this->payService->unifiedOrderApple($order_info);
                    break;
                case 6://手机网站支付宝H5
                    $result = $this->payService->unifiedWapAlipayH5($payConfig['config_name'], $order_info);
                    break;
                case 9://三方微信支付
                    $result = $this->payService->unifiedThreeWechat($order_info);
                    break;
                case 10://三方支付宝
                    $result = $this->payService->unifiedThreeAliPay($order_info);
                    break;
                case 11://cibn支付宝
                    $result = $this->payService->unifiedCibnAliPay($order_info);
                    break;
                case 12://微保支付宝
                    $result = $this->payService->unifiedweibaoAliPay($order_info);
                    break;
                case 13://盛付通支付宝
                    $result = $this->payService->unifiedSftAliPay($order_info);
                    break;
                case 14://盛付通微信
                    $result = $this->payService->unifiedSftWechat($order_info);
                    break;
                case 15://常富支付通手机网站支付
                    $result = $this->payService->unifiedZftH5($order_info);
                    break;
                case 19://ogw手机网站支付
                    $result = $this->payService->unifiedOgw($order_info);
                    break;
                case 20: //商富通H5支付宝
                    $result = $this->payService->SFTH5($order_info);
                    break;
                case 21://常富支付通手机网站支付
                    $result = $this->payService->unifiedZftApp($order_info);
                    break;
                case 25: //杉德快捷支付(支付宝)
                    $result = $this->payService->sendPayFast($order_info);
                    break;
                case 26: //杉德微信支付
                    $result = $this->payService->sendWeChatPayFast($order_info);
                default:
                    break;
            }
            if (empty($result)) {
                Response::returnResult(Hint::ERROR);
            }
            Response::returnResult(Hint::SUCCESS, $result);
        } catch (\Exception $e) {
            write_log('payy', $e->getMessage() . "\n" . $e->getTraceAsString(), []);
        } catch (\Error $e) {
            write_log('payy', $e->getMessage() . "\n" . $e->getTraceAsString(), []);
        }
    }

    /**
     * 首冲奖励订单创建&发起支付
     * @return void
     */
    public function pay_user_first_recharge_middleware()
    {
        $first_recharge_id = (int) $this->request->input('first_recharge_id');//礼包id
        $pay_type = (int) $this->request->input('pay_type');//支付类型	1=支付宝 2=微信 3=苹果 6=支付宝h5 7=微信H5 8=公众号
        $plat_type = (int) $this->request->input('plat_type');//客户端类型 1=iOS 2=android 3=apple内购 4=公众号 5=后台充值 6=H5
        $code = $this->request->input('code'); //微信公众鉴权code
        $custom_price = (int) $this->request->input('custom_price') ?? 0; //自定义充值金额
        $rechargeType = (int) $this->request->input('recharge_type',0); //充值类型 0=钻石 1=
        $rule_id = 0;

        //获取目前开启的支付类型
        $config = $this->model_system->get_config();

        $userId = $this->services_user->get_current_user_id()??0;
        if ($userId <= 0) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $list=$this->services_finance->setpayTypeList($userId);
        if(empty($list)){
          $list = empty($config['pay_type_list']) ? [] : $config['pay_type_list']['value'];
        }
        if(!in_array($pay_type, $list)  && $pay_type != 25){
            Response::returnResult(Hint::NOT_ALLOWED,[],'支付渠道维护中!');
        }

        //单比最多充值限制
        if($custom_price > 100000){
            Response::returnResult(Hint::FINANCE_RECHARGE_PAY_MAX);
        }

        //获取未实名用户充值限制
        $limited = $this->service->checkUserConfirmStatusByMoney($userId,$custom_price);
        if(!$limited){
            Response::returnResult(33,[],'如需充值请先实名认证!');
        }

        //白名单
        // $testUser = $this->userModel->get_test_users();
        // if(!in_array($userId,['10003'])){
        //     Response::returnResult(Hint::PARAM_ERROR, [], '支付维护中,请稍后~');
        // }

        //支付宝问题
        // if($pay_type == 1) {
        //     Response::returnResult(Hint::PARAM_ERROR, [], '充值请加代充微信：wsl55555888');
        // }
        // //微信问题
        // if($pay_type == 2) {
        //     Response::returnResult(Hint::PARAM_ERROR, [], '代充请加微信：wsl55555888');
        // }

        //充值完成间隔渠道判断
        if(!empty(Redis::GET($this->payOrderLimitReidsKey.$userId.':PAYTYPE:'.$pay_type))){
            //Response::returnResult(Hint::NOT_ALLOWED,[],'操作频繁，请稍后重试');
        }

        //充值未支付三次5分钟间隔判断
        if(!empty(Redis::GET($this->payOrderFiveMinutes.$userId.':PAYTYPE:'.$pay_type))){
            //Response::returnResult(Hint::NOT_ALLOWED,[],'请勿连续未支付订单，5分钟后可自动解除充值限制');
        }

        //充值永久黑名单
        $payBlack = $this->service->checkUserInRechargeBlack($userId, $pay_type);
        if($payBlack) {
            Response::returnResult(Hint::FINANCE_RECHARGE_BLACK,[],'当前支付通道异常，请您稍后再试');
        }

        if(!in_array($pay_type, [1,2,3,6,9,10,11,12,13,14,15,19])) $pay_type = 0;
        if ($first_recharge_id === null || empty($pay_type) || $plat_type == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        init_redis_session();
        if (!isset($_SESSION['user_id'])) {
            $this->service->checkToken();
        }
        if(!empty($custom_price)){

            if($custom_price < 6){
                Response::returnResult(Hint::PARAM_ERROR, [], '抱歉，自定义金额最低充值6元');
            }
            if($custom_price > 18){
                Response::returnResult(Hint::PARAM_ERROR, [], '抱歉，自定义金额最多充值18元');
            }
        }
        //根据pay_type读取流水id，和支付配置名称
        $payConfig = $this->payService->getConfigByPayType($pay_type);
        if(empty($payConfig)){
            Response::returnResult(Hint::FINANCE_RECHARGE_TYPE_ERROR);
        }
        $createRes = $this->service->createRechargeOrder($userId, $rule_id, $payConfig['stream_type'], $plat_type,$rechargeType,$custom_price, $payConfig['config_name'],$first_recharge_id);
        if(is_array($createRes) && !empty($createRes['orderId'])) {
            $order_id = $createRes['orderId'];
        }else{
            if (is_numeric($createRes) && $createRes > 0) {
                Response::returnResult($createRes);
            }else{
                Response::returnResult(Hint::WAIT_OPEN);
            }
        }

        $result = [];
        switch ($pay_type) {
            case 1://支付宝app
                $result = $this->payService->orderAlipayApp($payConfig['config_name'],$order_info);
                break;
            case 2://微信app
                $result = $this->payService->unifiedOrderWxApp($payConfig['config_name'],$order_info);
                break;
            case 3://苹果
                $result = $this->payService->unifiedOrderApple($order_info);
                break;
            case 6://手机网站支付宝H5
                $result = $this->payService->unifiedWapAlipayH5($payConfig['config_name'],$order_info);
                break;
            case 9://三方微信支付
                $result = $this->payService->unifiedThreeWechat($order_info);
                break;
            case 10://三方支付宝
                $result = $this->payService->unifiedThreeAliPay($order_info);
                break;
            case 11://cibn支付宝
                $result = $this->payService->unifiedCibnAliPay($order_info);
                break;
            case 12://微保支付宝
                $result = $this->payService->unifiedweibaoAliPay($order_info);
                break;
            case 13://盛付通支付宝
                $result = $this->payService->unifiedSftAliPay($order_info);
                break;
            case 14://盛付通微信
                $result = $this->payService->unifiedSftWechat($order_info);
                break;
            case 15://常富支付通手机网站支付
                $result = $this->payService->unifiedZftH5($order_info);
                break;
            case 19://ogw手机网站支付
                $result = $this->payService->unifiedOgw($order_info);
                break;
            default:
                break;
        }
        if (empty($result)) {
            Response::returnResult(Hint::ERROR);
        }
        Response::returnResult(Hint::SUCCESS, $result);
    }

    /**
     * 微信h5支付调转
     */
    public function referer()
    {
        $key = $this->request->get('key');
        if (empty($key)) {
            return;
        }
        $this->payService->referer($key);
    }

    /**
     * 无登录状态 通过用户ID（uuid）获取用户信息
     *
     * @param user_uid 用户UUID（生成的用户uuid，非自增user_id）
     * */
    public function getUserInfoByUuid()
    {
        $code = Hint::SUCCESS;
        $rest = [];

        do {
            $userUid = $this->request->post('user_uid') ?? '';
            if (empty($userUid)) {
                $code = Hint::PARAM_ERROR;
                break;
            }

            $rest = $this->services_user->getUserInfoByUuid($userUid);
            if (empty($rest)) {
                $code = Hint::PHONE_STATUS_1;
                break;
            }
        } while (false);

        Response::returnResult($code, $rest);
    }

    /**
     * @desc 微信公众号异步通知
     * @date 2019/1/4
     * */
    public function wx_mp_notify()
    {
        $data = $this->payService->wxmppay_notify_verify();
        write_log("pay", "wxmppay", $data);
        if ($data != null) {
            echo "<xml>
            <return_code><![CDATA[SUCCESS]]></return_code>
            <return_msg><![CDATA[OK]]></return_msg>
            </xml>";
            if ($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS') {
                $res = $this->service->notifyOp($data['out_trade_no'], $data['transaction_id']);
                if ($res) {
                    $this->services_msg->recharge($data['out_trade_no']);
                }
            }

        }
    }

    /**
     * 微信app异步通知
     */
    public function wx_notify()
    {
        $data = $this->payService->wxpay_notify_verify();
        write_log("pay", "wxpay", $data);
        if ($data != null) {
            echo "<xml>
            <return_code><![CDATA[SUCCESS]]></return_code>
            <return_msg><![CDATA[OK]]></return_msg>
            </xml>";
            if ($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS') {
                $res = $this->service->notifyOp($data['out_trade_no'], $data['transaction_id']);
                if ($res) {
                    $this->services_msg->recharge($data['out_trade_no']);
                }
            }

        }
    }


    /**
     * @desc 支付宝app异步通知 $this->service->notifyOp($order_sn = '', $out_trade_sn = '', $pay_type = '')
     * @date 2019/1/3
     * */
    public function alipay_notify()
    {
        $notify_data = $this->request->post();
        write_log("pay", "alipay", $notify_data);
        $data = $this->payService->alipayNotifyVerify($notify_data);
        if ($data != null) {
            echo "success";
            if ($data['trade_status'] == 'TRADE_SUCCESS') {
                $res = $this->service->notifyOp($data['out_trade_no'], $data['trade_no'], 1);
                if ($res) {
                    $this->services_msg->recharge($data['out_trade_no']);
                }
            }
        }
    }

    /**
     * @desc 支付宝h5支付回调
     * */
    public function alipay_h5_notify()
    {
        $notify_data = $this->request->post();
        write_log("pay", "alipay_h5_notify", $notify_data);
        $data = $this->payService->alipayH5NotifyVerify($notify_data);
        if ($data != null) {
            echo "success";
            if ($data['trade_status'] == 'TRADE_SUCCESS') {
                $res = $this->service->notifyOp($data['out_trade_no'], $data['trade_no'], 6);
                if ($res) {
                    $this->services_msg->recharge($data['out_trade_no']);
                }
            }
        }
    }


    /**
     * @desc 苹果回调
     * */
    public function apple_notify_verify()
    {
        $notify_data = $this->request->post();
        write_log("applePay", "applepay", $notify_data);
        $receipt_data = $this->request->post('apple_receipt');
        if (!isset($receipt_data)) {
            // write_log("applePay", "PARAM_ERROR",[]);
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $orders = $this->payService->apple_notify_order($receipt_data);
        // write_log("applePay", "orders", $orders);

        if (!empty($orders)) {
            foreach ($orders as $order) {
                $order_sn = null;
                $this->service->apple($order['user_id'], $order['rule_id'], $order['out_order_sn'], $order['channel_id'], $order_sn, $order['rule_recharge_type'] ?? 0);
                $this->services_msg->recharge($order_sn);
            }
        }
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 微信公众号登录
     */
    public function wx_mp_auth_url()
    {
        $appid = $this->config['wxmp_pay']['app_id'];
        $redirect_uri = $this->request->post('redirect_uri');
        $response_type = "code";
        $scope = "snsapi_base";
        $state = "STATE" . "#wechat_redirect";

        Response::returnResult(Hint::SUCCESS, "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . urlencode($redirect_uri) . "&response_type=" . $response_type . "&scope=" . $scope . "&state=" . $state);
    }


    /**
     * 后台代充值
     * @param Request $request
     */
    public function agent(Request $request)
    {
        $this->service->set_cross();
        $rule_id = (int)$request->post('rule_id', 0);
        $user_id = (int)$request->post('user_id', 0);

        // TODO 增加充值类型
        $rechargeType = $this->request->post('recharge_type', 0); // 充值类型 0=钻石,1=珍珠

        if (empty($rule_id) || empty($user_id)) {
            OutJson::outJsonResult(Hint::PARAM_ERROR);
        }
        $order_sn = null;
        $ret = $this->service->agent($user_id, $rule_id, $order_sn, $rechargeType);
        if (!$ret) {
            OutJson::outJsonResult(Hint::ERROR);
        }
        $this->services_msg->recharge($order_sn);
        OutJson::outJsonResult(Hint::SUCCESS);
    }


    /**
     * 支付宝H5白名单获取所有用户数组
     * @return mixed
     */
    public function getAliPayH5WhiteArr()
    {
        $userIdArr = $this->service->getAliPayH5WhiteArr();
        OutJson::outJsonResult(Hint::SUCCESS, $userIdArr);
    }

    /**
     * 支付宝H5白名单删除用户
     * @param $userId
     * @return mixed
     */
    public function delAliPayH5White(Request $request)
    {
        $userId = (int)$request->get('user_id') ?? 0;
        if (empty($userId)) {
            OutJson::outJsonNoData(Hint::ERROR);
            return;
        }
        $this->service->delAliPayH5White($userId);
        OutJson::outJsonNoData(Hint::SUCCESS);
    }


    /**
     * 支付宝H5白名单添加用户
     * @param $userId
     * @return mixed
     */
    public function addAliPayH5White(Request $request)
    {
        $userId = (int)$request->get('user_id') ?? 0;
        if (empty($userId)) {
            OutJson::outJsonNoData(Hint::ERROR);
            return;
        }
        $this->service->addAliPayH5White($userId);
        OutJson::outJsonNoData(Hint::SUCCESS);
    }

    /**
     * 支付宝H5白名单查找用户
     * @param $userId
     * @return mixed
     */
    public function findAliPayH5White(Request $request)
    {
        $userId = (int)$request->get('user_id') ?? 0;
        if (empty($userId)) {
            OutJson::outJsonNoData(Hint::ERROR);
            return;
        }
        $data = $this->service->findAliPayH5White($userId);
        OutJson::outJsonResult(Hint::SUCCESS, $data);
    }


    /**
     * 小度查找订单
     */
    public function h5Auto()
    {

    }

    /**
     * 获取充值历史记录
     */
    public function getRechargeUser()
    {
        $user_id = $this->request->get('user_id');
        if (empty($user_id)) {
            OutJson::outJsonNoData(Hint::ERROR);
            return;
        }
        $recharge_users = $this->service->getRechargeUsers($user_id);
        OutJson::outJsonResult(Hint::SUCCESS, $recharge_users);
    }

    //验证是否需要二次验证（实名） add by zxiaofee
    public function needReverify(Request $request)
    {
        $userId = $this->services_user->get_current_user_id();
        $checkResult = $this->payService->needReverify($userId);

        Response::returnResult(Hint::SUCCESS, ['reverify' => $checkResult]);
    }

    //三方微信支付回调
    public function threewx_notify()
    {
        $notify_data = $this->request->post();
        write_log("pay", "three_wx_notify", $notify_data);
        $data = $this->payService->three_notify_check($notify_data);
        if ($data) {
            echo '{"status":"1"}';
            $res = $this->service->notifyOp($notify_data['order_id'], $notify_data['order_id'], 9);
            if ($res) {
                $this->services_msg->recharge($notify_data['order_id']);
            }
        }
    }

    //三方支付宝支付回调
    public function threeali_notify()
    {
        $notify_data = $this->request->post();
        write_log("pay", "three_ali_notify", $notify_data);
        $data = $this->payService->three_notify_check($notify_data);
        if ($data) {
            echo '{"status":"1"}';
            $res = $this->service->notifyOp($notify_data['order_id'], $notify_data['order_id'], 10);
            if ($res) {
                $this->services_msg->recharge($notify_data['order_id']);
            }
        }
    }

    public function cibnali_notify()
    {
        $notify_data = $this->request->post();
        write_log("pay_notify", "bicn_ali", $notify_data);
        $data = $this->payService->cibn_notify_check($notify_data);
        if ($data) {
            echo 'success';
            $res = $this->service->notifyOp($notify_data['merchantTradeNo'], $notify_data['merchantTradeNo'], 11);
            if ($res) {
                $this->services_msg->recharge($notify_data['merchantTradeNo']);

                // 1分钟内充值成功超过5笔封号
                $order_info = $this->model_finance_recharge_order->getOrderBySn($notify_data['merchantTradeNo']);
                $user_id = $order_info['user_id'];
                $end_time = time();
                $start_time = time() - 60;
                $pay_count = $this->model_finance_recharge_order->getOrderCountByUser([$user_id], $start_time, $end_time);
                if ($pay_count >= 10) {
                    // 拉入黑名单
                    $this->services_black->userForbid($user_id, '1分钟内充值成功超过10笔');
                }

            }

        }

    }

    //微宝回调
    public function weibaoali_notify()
    {
        $notify_data = $this->request->post();
        write_log("pay_notify", "weibao_ali", $notify_data);
        if ($notify_data) {
            echo 'success';
            $res = $this->service->notifyOp($notify_data['merchantTradeNo'], $notify_data['platformOutTradeNo'], 12);
            if ($res) {
                $this->services_msg->recharge($notify_data['merchantTradeNo']);
                // 1分钟内充值成功超过5笔封号
                $order_info = $this->model_finance_recharge_order->getOrderBySn($notify_data['merchantTradeNo']);
                $user_id = $order_info['user_id'];
                $end_time = time();
                $start_time = time() - 60;
                $pay_count = $this->model_finance_recharge_order->getOrderCountByUser([$user_id], $start_time, $end_time);
                if ($pay_count >= 10) {
                    // 拉入黑名单
                    $this->services_black->userForbid($user_id, '1分钟内充值成功超过10笔');
                }

            }

        }

    }

    public function weibaoali_riskControlNotifyUrl()
    {
        $notify_data = $this->request->post();
        write_log("pay_notify1", "weibao_alifengkong", $notify_data);


    }

    //盛付通回调
    public function sftali_notify()
    {
        $notify_data = $this->request->post();
        write_log("pay_notify", "sft_ali", $notify_data);
        $data = $this->payService->sft_notify_check($notify_data);
        if ($data) {
            echo 'success';
            $res = $this->service->notifyOp($notify_data['mchOrderNo'], $notify_data['payOrderId'], 13);
            if ($res) {
                $this->services_msg->recharge($notify_data['mchOrderNo']);

                // 1分钟内充值成功超过5笔封号
                $order_info = $this->model_finance_recharge_order->getOrderBySn($notify_data['mchOrderNo']);
                $user_id = $order_info['user_id'];
                $end_time = time();
                $start_time = time() - 60;
                $pay_count = $this->model_finance_recharge_order->getOrderCountByUser([$user_id], $start_time, $end_time);
                if ($pay_count >= 10) {
                    // 拉入黑名单
                    $this->services_black->userForbid($user_id, '1分钟内充值成功超过10笔');
                }

            }

        }

    }

    //常富手机网站H5支付
    public function zftH5_notify()
    {
        $notify_data = $this->request->post();
        write_log("pay_zftH5notify", "zftH5_ali", $notify_data);
        $check_res = $this->payService->zftH5_notify_check($notify_data);
        if ($check_res) {
            echo 'success';
            $res = $this->service->notifyOp($notify_data['mch_orderid'], $notify_data['orderid'], 15);
            if ($res) {
                $this->services_msg->recharge($notify_data['mch_orderid']);

                // 1分钟内充值成功超过5笔封号
                $order_info = $this->model_finance_recharge_order->getOrderBySn($notify_data['mch_orderid']);
                $user_id = $order_info['user_id'];
                $end_time = time();
                $start_time = time() - 60;
                $pay_count = $this->model_finance_recharge_order->getOrderCountByUser([$user_id], $start_time, $end_time);
                if ($pay_count >= 10) {
                    // 拉入黑名单
                    $this->services_black->userForbid($user_id, '1分钟内充值成功超过10笔');
                }

            }

        }

    }

    //常富手机网站H5支付
    public function ogw_notify()
    {
        $notify_data = $this->request->post();
        write_log("pay_ogw_notify", "ogwH5_ali", $notify_data);
        $check_res = $this->payService->ogwH5_notify_check($notify_data);
        if ($check_res) {
            echo 'ok';
            $res = $this->service->notifyOp($notify_data['origOrderReqNo'], $notify_data['gateWayTradeNo'], 19);
            if ($res) {
                $this->services_msg->recharge($notify_data['origOrderReqNo']);

                // 1分钟内充值成功超过5笔封号
                $order_info = $this->model_finance_recharge_order->getOrderBySn($notify_data['origOrderReqNo']);
                $user_id = $order_info['user_id'];
                $end_time = time();
                $start_time = time() - 60;
                $pay_count = $this->model_finance_recharge_order->getOrderCountByUser([$user_id], $start_time, $end_time);
                if ($pay_count >= 10) {
                    // 拉入黑名单
                    $this->services_black->userForbid($user_id, '1分钟内充值成功超过10笔');
                }

            }

        }

    }

    //商福通回调
    public function shangfutong_h5_notify()
    {
        try {
            $notify_data = $this->request->post();
            write_log("pay_notify", "shangfutong_h5_notify", $notify_data);
            $bizData = $notify_data['bizData'];
            $bizData = json_decode($bizData, true);
            write_log("pay_notify", "bizData", $bizData);
            $check_res = $this->payService->shangfutong_h5_notify_check($bizData);
            write_log("pay_notify", "check_res" ,$check_res);
            if ($check_res) {
                echo 'SUCCESS';
                $res = $this->service->notifyOp($bizData['mchOrderNo'], $bizData['payOrderId'], 20);
                if ($res) {
                    $this->services_msg->recharge($bizData['mchOrderNo']);
                    // 1分钟内充值成功超过5笔封号
                    $order_info = $this->model_finance_recharge_order->getOrderBySn($bizData['mchOrderNo']);
                    $user_id = $order_info['user_id'];
                    $end_time = time();
                    $start_time = time() - 60;
                    $pay_count = $this->model_finance_recharge_order->getOrderCountByUser([$user_id], $start_time, $end_time);
                    if ($pay_count >= 10) {
                        // 拉入黑名单
                        $this->services_black->userForbid($user_id, '1分钟内充值成功超过10笔');
                    }

                }

            }
        } catch (\Exception $e) {
            write_log("pay_notify", "shangfutong_h5_notify_exception", [
                'message' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);
            die;
        } catch (\Error $e) {
            write_log("pay_notify", "shangfutong_h5_notify_error", [
                'message' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);
            die;
        }
    }


    /**
     * 杉德支付查询
     * @param Request $request
     * @return void
     */
    public function sendPayByNo(Request $request)
    {
        $no = (string)$request->post('no', 0);
        $ret = $this->payService->sendPayByNo($no);
        OutJson::outJsonResult(Hint::SUCCESS,$ret);
    }

    //杉德快捷支付回调
    public function sendpay_fast_notify()
    {
        try {
            //error_log("alialialiali:::::::::");
            $notify_data = $this->request->post();
            write_log("pay_notify", "sendpay_fast_notify", $notify_data);
            $bizData = $notify_data['bizData'];
            $bizData = json_decode($bizData, true);
            write_log("pay_notify", "bizData", $bizData);
            $check_res = $this->payService->sandpay_sdk_notify_check($bizData);
            write_log("pay_notify", "check_res" ,$check_res);
            if ($check_res) {
                //error_log("alialialiali:::::::::okokokokok");
                echo 'respCode=000000';
                $res = $this->service->notifyOp($bizData['outOrderNo'], $bizData['sandSerialNo'], 25);
                if ($res) {
                    $this->services_msg->recharge($bizData['outOrderNo']);
                    // 1分钟内充值成功超过5笔封号
                    $order_info = $this->model_finance_recharge_order->getOrderBySn($bizData['outOrderNo']);
                    $user_id = $order_info['user_id'];
                    $end_time = time();
                    $start_time = time() - 60;
                    $pay_count = $this->model_finance_recharge_order->getOrderCountByUser([$user_id], $start_time, $end_time);
                    if ($pay_count >= 10) {
                        // 拉入黑名单
                        $this->services_black->userForbid($user_id, '1分钟内充值成功超过10笔');
                    }

                }

            }
        } catch (\Exception $e) {
            write_log("pay_notify", "sendpay_fast_notify_exception", [
                'message' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);
            die;
        } catch (\Error $e) {
            write_log("pay_notify", "sendpay_fast_notify_error", [
                'message' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);
            die;
        }
    }

    //杉德微信支付回调
    public function sendpay_wechat_notify()
    {
        //error_log("wxw3xwxwxwx:::::::::");
        try {
            $notify_data = $this->request->post();
            write_log("pay_notify", "sendpay_wechat_notify", $notify_data);
            $bizData = $notify_data['bizData'];
            $bizData = json_decode($bizData, true);
            write_log("pay_notify", "bizData", $bizData);
            $check_res = $this->payService->sandpay_sdk_notify_check($bizData);
            write_log("pay_notify", "check_res" ,$check_res);
            if ($check_res) {
                echo 'respCode=000000';
                $res = $this->service->notifyOp($bizData['outOrderNo'], $bizData['sandSerialNo'], 26);
                if ($res) {
                    $this->services_msg->recharge($bizData['outOrderNo']);
                    // 1分钟内充值成功超过5笔封号
                    $order_info = $this->model_finance_recharge_order->getOrderBySn($bizData['outOrderNo']);
                    $user_id = $order_info['user_id'];
                    $end_time = time();
                    $start_time = time() - 60;
                    $pay_count = $this->model_finance_recharge_order->getOrderCountByUser([$user_id], $start_time, $end_time);
                    if ($pay_count >= 10) {
                        // 拉入黑名单
                        $this->services_black->userForbid($user_id, '1分钟内充值成功超过10笔');
                    }

                }

            }
        } catch (\Exception $e) {
            write_log("pay_notify", "sendpay_wechat_notify_exception", [
                'message' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);
            die;
        } catch (\Error $e) {
            write_log("pay_notify", "sendpay_wechat_notify_error", [
                'message' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);
            die;
        }
    }

}
